-- 战斗逻辑坐标转换为entitylayer的局部坐标

local LocationConfig = {}

local baseScale = 1

function LocationConfig.calculate(self, logicWidth, screenScale, characterPosX, characterLogicPosX, isSceneCenter)
	-- locationCount 战场逻辑宽度
	local battleFieldLogicWidth = logicWidth or 100000

	-- 屏幕相对于entity layer的缩放值
	self.screenScale = screenScale or 1.0
	
	self.logicWidth = battleFieldLogicWidth
	self.left = 1
	self.right = battleFieldLogicWidth - 1

	local offsetXOnEntityLayer = characterLogicPosX / Constant.BattleLogicToSceneScale * self.screenScale
	if isSceneCenter then
		offsetXOnEntityLayer = logicWidth * 0.5 / Constant.BattleLogicToSceneScale * self.screenScale
	end
	self.battleOriginXOnEntityLayer = characterPosX - offsetXOnEntityLayer
	self.offsetXOnEntityLayer = offsetXOnEntityLayer

	self:calculateHurtParams()

	return baseScale
end

function LocationConfig.getLogicBattleFieldWidth(self)
	return self.logicWidth
end

function LocationConfig.getScreenScale(self)
	return self.screenScale
end

function LocationConfig.getLeftPos(self)
	return self.left
end

function LocationConfig.getRightPos(self)
	return self.right
end

function LocationConfig.getLogicCenterPos(self)
	if self.right and self.left then
		return (self.right + self.left)/2.0
	else
		return nil
	end

end

function LocationConfig.getSpace(self)
	return 1 / Constant.BattleLogicToSceneScale * self.screenScale
end

function LocationConfig.getPosOnEntityLayer(self, pos)
	if pos then
		return self.battleOriginXOnEntityLayer + pos / Constant.BattleLogicToSceneScale * self.screenScale
	else
		return nil
	end
end

function LocationConfig.getPosOffsetXOnMap(self,posOnEntityLayer)
	local pos = (posOnEntityLayer-self.battleOriginXOnEntityLayer)*Constant.BattleLogicToSceneScale/self.screenScale
	return (pos-self.left)/(self.right-self.left)
end

function LocationConfig.getPosOnEntityLayerByPosOffset(self,offset)

	return self:getPosOnEntityLayer(offset*self.logicWidth)
	--offset*self.logicWidth/Constant.BattleLogicToSceneScale * self.screenScale - self.offsetXOnEntityLayer
end

function LocationConfig.getWidthOnEntityLayer(self,range)
	if range then
		return range / Constant.BattleLogicToSceneScale * self.screenScale
	else
		return nil
	end
end

function LocationConfig.calculateHurtParams(self)
	local space = self:getWidthOnEntityLayer(Constant.BattleHurtMoveLength)
    local backTime = Constant.BattleHurtMoveTime*Constant.BattleHurtBackTimeRate
    local frontTime = Constant.BattleHurtMoveTime - backTime
    
    --平均速度计算
    local backSpeed = space/backTime
    local frontSpeed = space/frontTime

	if not self._hurtMoveParams then
		self._hurtMoveParams = {}
	end

	self._hurtMoveParams.space = space
	self._hurtMoveParams.backTime = backTime
	self._hurtMoveParams.frontTime = frontTime
	self._hurtMoveParams.backSpeed = backSpeed
	self._hurtMoveParams.frontSpeed = frontSpeed
end

function LocationConfig.getHurtMoveParams(self)
	return self._hurtMoveParams
end

return LocationConfig